package com.example.common.recycler.base;

import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.view.GestureDetectorCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.recycler.base.BaseRecyclerAdapter;
import com.example.common.recycler.base.RecyclerItemClickListener;


/**
 * recyclerview item点击事件监听器
 * recyclerview adapter必须是BaseRecyclerAdapter的子类
 * 示例：
 * recyclerView.addOnItemTouchListener(new RecyclerItemClickListenerExt(recyclerView, new RecyclerItemClickListenerExt.OnItemClickListener() {...})
 *
 * @param <T> item的类型
 */
public class RecyclerItemClickListenerExt<T> extends RecyclerView.SimpleOnItemTouchListener {

    private OnItemClickListener mClickListener;
    private GestureDetectorCompat mGestureDetector;

    public interface OnItemClickListener<T> {

        void onItemClick(MotionEvent e, View view, int position, @Nullable T item);

        void onItemLongClick(MotionEvent e, View view, int position, @Nullable T item);

    }

    public static class SimpleOnItemClickListener<T> implements OnItemClickListener<T> {

        @Override
        public void onItemClick(MotionEvent e, View view, int position, @Nullable T item) {
        }

        @Override
        public void onItemLongClick(MotionEvent e, View view, int position, @Nullable T item) {
        }

    }

    public RecyclerItemClickListenerExt(final RecyclerView recyclerView,
                                        OnItemClickListener<T> listener) {
        this.mClickListener = listener;
        mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(),
                new RecyclerItemClickListener.SimpleOnGestureListener() {//使用GestureDetector.SimpleOnGestureListener的话会导致响应双击事件
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                        if (childView != null && mClickListener != null) {
                            int pos = recyclerView.getChildAdapterPosition(childView);
                            if (pos >= 0) {
                                T t = getItem(pos, recyclerView);
                                mClickListener.onItemClick(e, childView, pos, t);
                            }
                        }
                        return true;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                        if (childView != null && mClickListener != null) {
                            int pos = recyclerView.getChildAdapterPosition(childView);
                            if (pos >= 0) {
                                T t = getItem(pos, recyclerView);
                                mClickListener.onItemLongClick(e, childView, pos, t);
                            }
                        }
                    }
                });
    }

    private T getItem(int pos, RecyclerView recyclerView) {
        if (recyclerView.getAdapter() instanceof BaseRecyclerAdapter) {
            BaseRecyclerAdapter baseRecyclerAdapter = (BaseRecyclerAdapter) recyclerView.getAdapter();
            return (T) baseRecyclerAdapter.getItem(pos);
        }
        return null;
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
        return false;
    }
}